/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.jface.preference.ColorSelector;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * Defines the color section of uml elements.
 * 
 * @author Gilles Cannenterre
 * @since 28 nov. 06
 */
public class FigureColorsSection extends PropertyViewSection {

    /**
     * The background color selector.
     */
    protected ColorSelector backgroundColorSelector;

    /**
     * The border color selector.
     */
    protected ColorSelector borderColorSelector;

    /**
     * The font color selector.
     */
    protected ColorSelector fontColorSelector;

    /**
     * The background color label.
     */
    protected CLabel backgroundColorLabel;

    /**
     * The border color label.
     */
    protected CLabel borderColorLabel;

    /**
     * The font color label.
     */
    protected CLabel fontColorLabel;

    /**
     * The background color button listener.
     */
    protected IPropertyChangeListener backgroundColorButtonListener;

    /**
     * The border color button listener.
     */
    protected IPropertyChangeListener borderColorButtonListener;

    /**
     * The font color button listener.
     */
    protected IPropertyChangeListener fontColorButtonListener;

    /**
     * Sets the input.
     * 
     * @param part
     *            the part
     * @param selection
     *            the selection
     */
    @Override
    public void setInput(IWorkbenchPart part, ISelection selection) {
        super.setInput(part, selection);
    }

    /**
     * Creates the controls.
     * 
     * @param tabbedPropertySheetPage
     *            the tabbed property sheet page
     * @param parent
     *            the parent
     * @see org.eclipse.ui.views.properties.tabbed.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite,
     *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
     */
    @Override
    public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
        super.createControls(parent, tabbedPropertySheetPage);
        final Composite composite = getWidgetFactory().createFlatFormComposite(parent);
        FormData data;

        // Background Color
        backgroundColorSelector = new ColorSelector(composite);
        data = new FormData();
        data.left = new FormAttachment(0, 105);
        data.top = new FormAttachment(0);
        backgroundColorSelector.getButton().setLayoutData(data);

        backgroundColorButtonListener = new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (graphicalElement != null) {
                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeEditPart) graphicalElement).getGraphNode().setBackgroundColor(
                                backgroundColorSelector.getColorValue());
                    } else if (graphicalElement instanceof GraphEdgeEditPart) {
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setBackgroundColor(
                                backgroundColorSelector.getColorValue());
//                    } else if (graphicalElement instanceof SeDiSubpartEditPart
//                            || graphicalElement instanceof AbstractLinkEditPart) {
//                        if (graphicalElement.getModel() instanceof GraphElement) {
//                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
//                            graphElement
//                                    .setBackgroundColor(backgroundColorSelector.getColorValue());
//                        }
                    }
                }
            }
        };
        backgroundColorSelector.addListener(backgroundColorButtonListener);

        // Border Color
        borderColorLabel = getWidgetFactory().createCLabel(composite, "Border Color:"); //$NON-NLS-1$
        data = new FormData();
        data.left = new FormAttachment(backgroundColorSelector.getButton(), 40);
        data.top = new FormAttachment(backgroundColorSelector.getButton(), 1, SWT.CENTER);
        borderColorLabel.setLayoutData(data);

        borderColorSelector = new ColorSelector(composite);
        data = new FormData();
        data.left = new FormAttachment(borderColorLabel, 0);
        data.top = new FormAttachment(0);
        borderColorSelector.getButton().setLayoutData(data);

        borderColorButtonListener = new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (graphicalElement != null) {
                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeEditPart) graphicalElement).getGraphNode().setBorderColor(
                                borderColorSelector.getColorValue());
                    } else if (graphicalElement instanceof GraphEdgeEditPart) {
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setBorderColor(
                                borderColorSelector.getColorValue());
//                    } else if (graphicalElement instanceof SeDiSubpartEditPart
//                            || graphicalElement instanceof AbstractLinkEditPart) {
//                        if (graphicalElement.getModel() instanceof GraphElement) {
//                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
//                            graphElement.setBorderColor(borderColorSelector.getColorValue());
//                        }
                    }
                }
            }
        };
        borderColorSelector.addListener(borderColorButtonListener);

        // Font Color
        fontColorLabel = getWidgetFactory().createCLabel(composite, "Font Color:"); //$NON-NLS-1$
        data = new FormData();
        data.left = new FormAttachment(borderColorSelector.getButton(), 40);
        data.top = new FormAttachment(borderColorSelector.getButton(), 1, SWT.CENTER);
        fontColorLabel.setLayoutData(data);

        fontColorSelector = new ColorSelector(composite);
        data = new FormData();
        data.left = new FormAttachment(fontColorLabel, 0);
        data.top = new FormAttachment(0);
        fontColorSelector.getButton().setLayoutData(data);

        fontColorButtonListener = new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                if (graphicalElement != null) {
                    if (graphicalElement instanceof GraphNodeEditPart) {
                        ((GraphNodeEditPart) graphicalElement).getGraphNode().setFontColor(
                                fontColorSelector.getColorValue());
                    } else if (graphicalElement instanceof GraphEdgeEditPart) {
                        ((GraphEdgeEditPart) graphicalElement).getGraphEdge().setFontColor(
                                fontColorSelector.getColorValue());
//                    } else if (graphicalElement instanceof SeDiSubpartEditPart
//                            || graphicalElement instanceof AbstractLinkEditPart) {
//                        if (graphicalElement.getModel() instanceof GraphElement) {
//                            GraphElement graphElement = (GraphElement) graphicalElement.getModel();
//                            graphElement.setFontColor(fontColorSelector.getColorValue());
//                        }
                    }
                }
            }
        };
        fontColorSelector.addListener(fontColorButtonListener);

        backgroundColorLabel = getWidgetFactory().createCLabel(composite, "Background Color:"); //$NON-NLS-1$
        data = new FormData();
        data.left = new FormAttachment(0);
        data.right = new FormAttachment(backgroundColorSelector.getButton(), 0);
        data.top = new FormAttachment(backgroundColorSelector.getButton(), 1, SWT.CENTER);
        backgroundColorLabel.setLayoutData(data);
    }

    /**
     * Refresh.
     */
    @Override
    public void refresh() {
        if (!backgroundColorSelector.getButton().isDisposed()
                && !borderColorSelector.getButton().isDisposed()
                && !fontColorSelector.getButton().isDisposed()) {

            // Remove all listeners
            backgroundColorSelector.removeListener(backgroundColorButtonListener);
            borderColorSelector.removeListener(borderColorButtonListener);
            fontColorSelector.removeListener(fontColorButtonListener);

            if (graphicalElement != null) {
                RGB backgroundColor = null;
                RGB borderColor = null;
                RGB fontColor = null;
                if (graphicalElement instanceof GraphNodeEditPart) {
                    backgroundColorSelector.setEnabled(true);
                    borderColorSelector.setEnabled(true);
                    backgroundColor =
                            ((GraphNodeEditPart) graphicalElement).getGraphNode()
                                    .getBackgroundColor();
                    borderColor =
                            ((GraphNodeEditPart) graphicalElement).getGraphNode().getBorderColor();
                    fontColor =
                            ((GraphNodeEditPart) graphicalElement).getGraphNode().getFontColor();
                } else if (graphicalElement instanceof GraphEdgeEditPart) {
                    backgroundColorSelector.setEnabled(false);
                    borderColorSelector.setEnabled(false);
                    backgroundColor =
                            ((GraphEdgeEditPart) graphicalElement).getGraphEdge()
                                    .getBackgroundColor();
                    borderColor =
                            ((GraphEdgeEditPart) graphicalElement).getGraphEdge().getBorderColor();
                    fontColor =
                            ((GraphEdgeEditPart) graphicalElement).getGraphEdge().getFontColor();
//                } else if (graphicalElement instanceof SeDiSubpartEditPart
//                        || graphicalElement instanceof AbstractLinkEditPart) {
//                    if (graphicalElement.getModel() instanceof GraphElement) {
//                        GraphElement graphElement = (GraphElement) graphicalElement.getModel();
//                        backgroundColorSelector.setEnabled(true);
//                        borderColorSelector.setEnabled(true);
//                        backgroundColor = graphElement.getBackgroundColor();
//                        borderColor = graphElement.getBorderColor();
//                        fontColor = graphElement.getFontColor();
//                    }
                }
                if (backgroundColor != null) {
                    backgroundColorSelector.setColorValue(backgroundColor);
                }
                if (borderColor != null) {
                    borderColorSelector.setColorValue(borderColor);
                }
                if (fontColor != null) {
                    fontColorSelector.setColorValue(fontColor);
                }
            }

            // Add all listeners
            backgroundColorSelector.addListener(backgroundColorButtonListener);
            borderColorSelector.addListener(borderColorButtonListener);
            fontColorSelector.addListener(fontColorButtonListener);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
     */
    /**
     * 
     */
    public void dispose() {
        super.dispose();
        if (backgroundColorSelector != null && backgroundColorSelector.getButton() != null
                && !backgroundColorSelector.getButton().isDisposed())
            backgroundColorSelector.removeListener(backgroundColorButtonListener);
        if (borderColorSelector != null && borderColorSelector.getButton() != null
                && !borderColorSelector.getButton().isDisposed())
            borderColorSelector.removeListener(borderColorButtonListener);
        if (fontColorSelector != null && fontColorSelector.getButton() != null
                && !fontColorSelector.getButton().isDisposed())
            fontColorSelector.removeListener(fontColorButtonListener);
    }
}